package org.jim.core.utils;

import org.springframework.beans.factory.annotation.Value;

public class WebPropUtil {

    /**
     * IP地址
     */
    public static String bindIp = "0.0.0.0";
    /**
     * 监听端口
     */
    public static Integer bindPort = 0;
    /**
     *  SSL配置是否开启
     */
    public static String ssl = "off";
    /**
     * SSL位置
     */
    public static String path;
    /**
     * SSL密钥
     */
    public static String passwd;
    /**
     * 心跳包发送时长heartbeatTimeout/2
     */
    public static long heartbeatTimeout = 0;
    /**
     * redisIp
     */
    public static String redisIP;
    /**
     * redisPort
     */
    public static Integer redisPort;
    /**
     * redisPwd
     */
    public static String redisPwd;
    /**
     * redisOpen
     */
    public static Integer redisOpen;
    /**
     * redisDB
     */
    public static Integer redisDB;
    /**
     * start
     */
    public static Boolean start = false;


    public static String getBindIp() {
        return bindIp;
    }

    public static Integer getBindPort() {
        if(Integer.valueOf(0).equals(bindPort)){
            return PropUtil.getInt("jim.port");
        }
        return bindPort;
    }

    public static String getSsl() {
        if("0".equals(ssl)){
            return PropUtil.get("jim.cluster");
        }
        return ssl;
    }

    public static String getPath() {
        if("0".equals(path)){
            return PropUtil.get("jim.key.store.path");
        }
        return path;
    }

    public static String getPasswd() {
        if("0".equals(passwd)){
            return PropUtil.get("jim.key.store.pwd");
        }
        return passwd;
    }

    public static long getHeartbeatTimeout() {
        return heartbeatTimeout;
    }

    public static String getRedisIP() {
        if("0".equals(redisIP)){
            return PropUtil.get("jim.redis.host");
        }
        return redisIP==null?"localhost":redisIP;
    }

    public static String getRedisPwd() {
        if("0".equals(redisPwd)){
            return PropUtil.get("jim.redis.auth");
        }
        return redisPwd==null?"":redisPwd;
    }

    public static Integer getRedisPort() {
        if("0".equals(redisPort)){
            return PropUtil.getInt("jim.redis.port");
        }
        return redisPort==null?6379:redisPort;
    }

    public static String getRedisOpen() {
        if(Integer.valueOf(0).equals(redisOpen)){
            return "off";
        }else {
            return "on";
        }
    }

    public static Integer getRedisDB() {
        return redisDB;
    }

    @Value("${jim.redis.db:0}")
    public void setRedisDB(Integer redisDB) {
        WebPropUtil.redisDB = redisDB;
    }

    @Value("${jim.redis.status:0}")
    public void setRedisOpen(Integer redisOpen) {
        WebPropUtil.redisOpen = redisOpen;
    }

    @Value("${jim.redis.port:0}")
    public void setRedisPort(Integer redisPort) {
        WebPropUtil.redisPort = redisPort;
    }

    @Value("${jim.redis.ip:0}")
    public void setRedisIP(String redisIP) {
        WebPropUtil.redisIP = redisIP;
    }

    @Value("${jim.redis.pwd:0}")
    public void setRedisPwd(String redisPwd) {
        WebPropUtil.redisPwd = redisPwd;
    }

    @Value("${jim.server.ip:0}")
    public void setBindIp(String bindIp) {
        WebPropUtil.bindIp = bindIp;
    }

    @Value("${jim.server.port:0}")
    public void setBindPort( Integer bindPort) {
        WebPropUtil.bindPort = bindPort;
    }

    @Value("${jim.ssl:0}")
    public void setSsl( String ssl) {
        WebPropUtil.ssl = ssl;
    }

    @Value("${jim.key.store.path:0}")
    public void setPath(String path) {
        WebPropUtil.path = path;
    }

    @Value("${jim.key.store.pwd:0}")
    public void setPasswd(String passwd) {
        WebPropUtil.passwd = passwd;
    }

    @Value("${jim.heartbeat.timeout:0}")
    public void setHeartbeatTimeout(Long heartbeatTimeout) {
        WebPropUtil.heartbeatTimeout = heartbeatTimeout;
    }

    @Value("${jim:0}")
    public void setStart() {
        WebPropUtil.start = true;
    }
}
